package com.cskaoyan.market.config;

import com.cskaoyan.market.filter.CharEncodingFilter;
import com.cskaoyan.market.filter.CorsFilter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;

/**
 * @Author: 鲸落
 * @Date: 2024/05/18/14:24
 * @Version: 1.0
 * @Description:
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    // JacksonUtil工具类的时间转换
    @Bean
    public ObjectMapper objectMapper(){
        String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
        String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
        String DEFAULT_TIME_FORMAT = "HH:mm:ss";
        ObjectMapper objectMapper = new ObjectMapper();
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        // 增加LocalDate和LocalTime和LocalDateTime的解析格式的支持
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));
        javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)));
        javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));
        javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)));
        javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));
        objectMapper.registerModule(javaTimeModule);
        return objectMapper;
    }
    // 注册配置filter
    @Bean
    public FilterRegistrationBean charEncodingFilter(){
        FilterRegistrationBean<CharEncodingFilter> bean = new FilterRegistrationBean<>();
        bean.setFilter(new CharEncodingFilter());
        bean.setUrlPatterns(Arrays.asList("/admin/*"));
        bean.setOrder(1);
        return bean;
    }
    @Bean
    public FilterRegistrationBean corsFilter(){
        FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>();
        bean.setFilter(new CorsFilter());
        bean.setUrlPatterns(Arrays.asList("/admin/*"));
        bean.setOrder(2);
        return bean;
    }
    // 文件上传路径，自己在自己的配置文件中改
    @Value("${upload.path}")
    private String uploadPath;
    // 配置静态资源处理器
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // todo 待处理问题：如果找不到要访问的静态资源，此时怎么办
        registry.addResourceHandler("/wx/storage/fetch/**").addResourceLocations("file:"+uploadPath);
    }
}